// pages/todayStop/todayStop.js
let app = getApp();
let util = require("../../utils/util");
import { getMonthBillApi, getParkProfitDayApi } from "../../api/index";
import { formatDate } from "../../utils/formatDate.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight - 120,
    dayList: [],
    sumMoney: "",
    countBill: "",
    type: null,
    startTime: null,
    endTime: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.type === "day") {
      const { startTime, endTime } = util.getDayStartAndEndTimestamp();
      this.setData({
        startTime,
        endTime,
      });
    } else {
      const { startTime, endTime } = util.getFirstAndLastDay();
      this.setData({
        startTime,
        endTime,
      });
    }
    let parkId = wx.getStorageSync("parkId") ? wx.getStorageSync("parkId") : "";
    this.setData({ parkId, type: options.type });
    // this.getMonthBill();
    this.getDayBill();
  },
  // 日期变更
  changeDate(e) {
    this.setData({
      startTime: e.detail.startTimestamp,
      endTime: e.detail.endTimestamp,
    });
    this.getMonthBill();
  },

  // 获取账单明细
  async getMonthBill() {
    try {
      const { parkId, startTime, endTime } = this.data;
      const { data: res } = await getMonthBillApi({
        parkId,
        startTime,
        endTime,
      });
      res.data.dayList.forEach((item) => {
        item.startTime = formatDate(item.nowDate);
        item.leftValue = item.countBill;
        item.rightValue = item.sumMoney;
      });
      this.setData({
        countBill: res.data.countBill,
        sumMoney: res.data.sumMoney,
        dayList: res.data.dayList,
      });
    } catch (error) {
      console.error(error);
    }
  },

  // 获取某天盈利
  async getDayBill() {
    try {
      const { parkId, startTime, endTime } = this.data;
      const { data: res } = await getParkProfitDayApi({
        parkId,
        startTime,
        endTime,
			});
			let dayList = []
			dayList.push(res.data)
      dayList.forEach((item) => {
        item.startTime = formatDate(item.date);
        item.leftValue = item.dayNumber;
        item.rightValue = item.dayAmount;
      });
      this.setData({
        dayList
      });
    } catch (error) {
      console.error(error);
    }
  },
});
